package com.example.makerspace.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.makerspace.dao.MeetingCategoryDAO;
import com.example.makerspace.dao.MeetingDAO;
import com.example.makerspace.dao.UserDAO;
import com.example.makerspace.domain.MeetingCategoryDO;
import com.example.makerspace.domain.MeetingDO;
import com.example.makerspace.domain.UserDO;
import com.example.makerspace.dto.MeetingCategoryDTO;
import com.example.makerspace.dto.MeetingDTO;
import com.example.makerspace.dto.PageRequest;
import com.example.makerspace.dto.Result;
import com.example.makerspace.service.MeetingService;
import com.example.makerspace.utils.FileUtils;
import com.example.makerspace.utils.JwtUtils;
import com.example.makerspace.utils.TLSSigAPIv2;
import com.example.makerspace.utils.ValidateCodeUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class MeetingServiceImpI implements MeetingService {

    private  static  final  String  MEET_PICTURE_PATH_PREFIX="meet/";

  private  final  static  String   testHttpPrefix="https://119.45.199.13:3035/static/";
   private  final  static  String  devHttpPrefix="https://nism.ytcata.com:443/static/";

    @Resource
    private MeetingDAO  meetingDAO;

    @Resource
    private UserDAO userDAO;

    @Resource
    private MeetingCategoryDAO meetingCategoryDAO;


    @Override
    public Result getMeetings(PageRequest pageRequest) {
        Map<String, Object> params = pageRequest.getParams();
        String category = MapUtils.getString(params, "category");
        String classify = MapUtils.getString(params, "classify");
        if (category.equals("") && classify.equals("")) {
            IPage<MeetingDO> meetingDOIPage = meetingDAO.selectByPage(pageRequest.getPagination());
            return Result.success(meetingDOIPage);
        } else if (!category.equals("") && classify.equals("")) {
            IPage<MeetingDO> meetingDOIPage = meetingDAO.selectByPageAndCategory(pageRequest.getPagination(), category);
            return Result.success(meetingDOIPage);
        } else if (category.equals("") && !classify.equals("")) {
            IPage<MeetingDO> meetingDOIPage = meetingDAO.selectByPageAndClassify(pageRequest.getPagination(), classify);
            return Result.success(meetingDOIPage);
        }else {
            IPage<MeetingDO> meetingDOIPage = meetingDAO.selectByPageCategoryAndClassify(pageRequest.getPagination(),category, classify);
            return Result.success(meetingDOIPage);
        }
    }

    @Override
    public Result addMeetings(MeetingDTO meetingDTO) {
        MeetingDO meetingDO = new MeetingDO();
        BeanUtils.copyProperties(meetingDTO,meetingDO);
        String videsName = ValidateCodeUtils.generateCode(4);
        if (!ObjectUtils.isEmpty(meetingDTO.getPhoto())) {
            String imagePath = FileUtils.saveMultipartFile2(meetingDTO.getPhoto(), devHttpPrefix+ MEET_PICTURE_PATH_PREFIX + videsName,
                    FileUtils.staticPath + MEET_PICTURE_PATH_PREFIX + videsName);
            meetingDO.setPhoto(imagePath);
        }
        meetingDAO.insert(meetingDO);
        return Result.success("添加成功!");
    }

    @Override
    public Result updateMeetings(MeetingDTO meetingDTO) {
        MeetingDO meetingDO = new MeetingDO();
        BeanUtils.copyProperties(meetingDTO,meetingDO);
        String videsName = ValidateCodeUtils.generateCode(4);
        if (!ObjectUtils.isEmpty(meetingDTO.getPhoto())) {
            String imagePath = FileUtils.saveMultipartFile2(meetingDTO.getPhoto(), devHttpPrefix + MEET_PICTURE_PATH_PREFIX + videsName,
                    FileUtils.staticPath + MEET_PICTURE_PATH_PREFIX + videsName);
            meetingDO.setPhoto(imagePath);
        }
        meetingDAO.updateById(meetingDO);
        return Result.success("修改成功!");
    }

    @Override
    public Result deleteMeetings(Integer id) {
        boolean b = meetingDAO.deleteById(id);
        if(b){
            return Result.success("删除成功!");
        }
        return Result.success("删除失败!");
    }

    @Override
    public Result getAllMeetings(PageRequest pageRequest) {
        IPage<MeetingDO> meetingDOIPage = meetingDAO.selectByPage(pageRequest.getPagination());
        return Result.success(meetingDOIPage);
    }

    @Override
    public Result clickBooking(Integer id,HttpServletRequest request) {
        String username = JwtUtils.getUsername(request);
        UserDO userDO = userDAO.selectByUsername(username);
        List<String> meetings = userDO.getMeetings();
        MeetingDO meetingDO = meetingDAO.selectById(id);
        meetingDO.setBookNumber(meetingDO.getBookNumber()+1);
        meetingDAO.updateById(meetingDO);
        if(!CollectionUtils.isEmpty(meetings)){
            if(meetings.contains(String.valueOf(id))){
                return  Result.success("您已经预约过了,请勿重复预约!");
            }
            meetings.add(String.valueOf(id));
            userDO.setMeetings(meetings);
            userDAO.updateById(userDO);
            return  Result.success("预约成功");
        }else {
            ArrayList<String> arr = new ArrayList<>();
            arr.add(String.valueOf(id));
            userDO.setMeetings(arr);
            userDAO.updateById(userDO);
            return Result.success("预约成功");
        }
    }

    @Override
    public Result recommend() {
        List<MeetingDO> meetingDOS = meetingDAO.selectByViews();
        return Result.success(meetingDOS);
    }

    @Override
    public Result cancelBooking(Integer id, HttpServletRequest request) {
        String username = JwtUtils.getUsername(request);
        UserDO userDO = userDAO.selectByUsername(username);
        List<String> meetings = userDO.getMeetings();
        meetings.remove(String.valueOf(id));
        userDO.setMeetings(meetings);
        userDAO.updateById(userDO);
        MeetingDO meetingDO = meetingDAO.selectById(id);
        meetingDO.setBookNumber(meetingDO.getBookNumber()-1);
        meetingDAO.updateById(meetingDO);
        return Result.success("取消预约成功");
    }

    @Override
    public Result getDetailMeeting(Integer id) {
        MeetingDO meetingDO = meetingDAO.selectById(id);
        return Result.success(meetingDO);
    }

    @Override
    public Result getMeetingCategory() {
        List<MeetingCategoryDO> meetingCategoryDOS = meetingCategoryDAO.selectAll();
        return  Result.success(meetingCategoryDOS);
    }

    @Override
    public Result getMeetingAllCategory(PageRequest pageRequest) {
        IPage<MeetingCategoryDO> meetingCategoryDOIPage = meetingCategoryDAO.selectByPage(pageRequest.getPagination());
        return Result.success(meetingCategoryDOIPage);
    }

    @Override
    public Result addMeetingCategory(MeetingCategoryDTO meetingCategoryDTO) {
        MeetingCategoryDO meetingCategoryDO = new MeetingCategoryDO();
        BeanUtils.copyProperties(meetingCategoryDTO,meetingCategoryDO);
        meetingCategoryDAO.insert(meetingCategoryDO);
        return Result.success("分类添加成功!");
    }

    @Override
    public Result updateMeetingCategory(MeetingCategoryDTO meetingCategoryDTO) {
        MeetingCategoryDO meetingCategoryDO = new MeetingCategoryDO();
        BeanUtils.copyProperties(meetingCategoryDTO,meetingCategoryDO);
        meetingCategoryDAO.updateById(meetingCategoryDO);
        return Result.success("修改分类成功!");
    }

    @Override
    public Result deleteMeetingCategory(Integer id) {
        boolean b = meetingCategoryDAO.deleteById(id);
        if(b){
            return Result.success("删除分类成功!");
        }else {
            return  Result.success("删除分类失败!");
        }

    }

    @Override
    public Result getUserSig(String username) {
        TLSSigAPIv2 api = new TLSSigAPIv2(1600042458, "394e37987e296add7874c4fbcc095319d7cf477dd93220fd12cc1a7627a549f9");
        String userSig = api.genUserSig(username, 180 * 86400);
        return  Result.success(userSig);
    }

    @Override
    public Result getUserList(Integer id) {
        return null;
    }
}
